<?php
/**
 * @version 1.0
 * @author jonathan <jo.link.noir@gmail.com>
 * @date 27-10-2009
 */
class SW_Resource_Element extends SW_Element_Group
{
	/** @var int */
	protected $quantity;
	/** @var int */
	protected $time_end;
	/** @var int */
	protected $per_hour;


	/**
	 * @param int $id
	 * @param int $group
	 * @param int $quantity = 0
	 * @param int $time_end = NULL
	 * @param int $per_hour = 0
	 */
	public function __construct($id, $group, $quantity = 0, $time_end = NULL, $per_hour = 0)
	{
		parent::__construct($id, $group);
		$this->quantity = (int)$quantity;
		$this->time_end = $time_end ? (int)$time_end : $_SERVER['REQUEST_TIME'];
		$this->per_hour = (int)$per_hour;
	}

	/**
	 * Augmente la quantité de ressource
	 * @param int $increase
	 */
	public function increase($increase)
	{
		$this->quantity += $increase;
	}

	/**
	 * Diminue la quantité de ressource
	 * @param int $decrease
	 */
	public function decrease($decrease)
	{
		$this->quantity -= $decrease;
	}

	/**
	 * @param int $per_hour
	 */
	public function setPerHour($per_hour)
	{
		$this->per_hour = $per_hour;
	}

	/**
	 * @param int $quantity
	 */
	public function setQuantity($quantity)
	{
		$this->quantity = $quantity;
	}

	/**
	 * @param int $time_end
	 */
	public function setTimeEnd($time_end)
	{
		$this->quantity += round(($time_end - $this->time_end) * $this->per_hour / 3600);
		$this->time_end = $time_end;
	}

	/**
	 * @param int $this->per_hour
	 */
	public function getPerHour()
	{
		return $this->per_hour;
	}

	/**
	 * @param int $this->quantity
	 */
	public function getQuantity()
	{
		return $this->quantity;
	}

	/**
	 * @param int $this->time_end
	 */
	public function getTimeEnd()
	{
		return $this->time_end;
	}

	/**
	 * Retourne la quantity de ressource au moment $date.
	 * @attention une valeur inferieur à time_end retournera une valeur négatif
	 * @param int $date
	 * @return int
	 */
	public function get($date)
	{
		return (int)($this->quantity + ($date - $this->time_end) * $this->per_hour / 3600);
	}

	/**
	 * Retourne la valeur actuelle de la resssource sous la forme d'une chaine de caractère
	 * @return string
	 */
	public function __toString()
	{
		return (string)$this->get($_SERVER['REQUEST_TIME']);
	}
}